package com.way.gesture;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibrary;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.way.util.AppUtil;
import com.way.util.GestureLibraryManager;

public class EditGestureActivity extends Activity {
	private String mGestureName;
	private GestureLibraryManager mGestureLibraryManager;
	private Gesture mGesture;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_gesture);
		AppUtil.transWindows(this,
				getResources().getColor(R.color.material_teal));
		getActionBar().setDisplayHomeAsUpEnabled(true);
		mGestureName = getIntent().getStringExtra("gestureName");

		TextView titleView = (TextView) findViewById(R.id.gesture_edit_name);
		AppUtil.setName(this, titleView, mGestureName);

		ImageView gestureImView = (ImageView) findViewById(R.id.gestures_imageview);
		mGestureLibraryManager = GestureLibraryManager.getInstance(this);
		if (mGestureLibraryManager.load()) {
			mGesture = mGestureLibraryManager.getGesture(mGestureName);
			final Bitmap bitmap = mGesture.toBitmap(280, 280, 8, Color.BLACK);
			gestureImView.setImageBitmap(bitmap);

		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void deleteGesture(View v) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.gestures_delete)
				.setMessage(R.string.gestures_delete_toast)
				.setNegativeButton(R.string.cancel_action, null)
				.setPositiveButton(R.string.rename_action,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								mGestureLibraryManager
										.removeGesture(mGestureName);
								finish();
								Toast.makeText(EditGestureActivity.this,
										R.string.gestures_delete_success,
										Toast.LENGTH_SHORT).show();
							}
						}).create().show();
	}

	public void editGesture(View view) {
		// Create a PopupMenu, giving it the clicked view for an anchor
		PopupMenu popup = new PopupMenu(this, view);

		// Inflate our menu resource into the PopupMenu's Menu
		popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());

		// Set a listener so we are notified if a menu item is clicked
		popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem menuItem) {
				switch (menuItem.getItemId()) {
				case R.id.menu_gestures_edit_image:
					Intent intent = new Intent(EditGestureActivity.this,
							CreateGestureActivity.class);
					intent.putExtra("task", mGestureName);
					intent.setAction("com.way.edit");
					startActivity(intent);
					finish();
					return true;
				case R.id.menu_gestures_edit_name:
					Intent intent1 = new Intent(EditGestureActivity.this,
							SelectTaskActivity.class);
					intent1.putExtra("task", mGestureName);
					intent1.setAction("com.way.edit");
					startActivity(intent1);
					finish();
					return true;
				}
				return false;
			}
		});

		// Finally show the PopupMenu
		popup.show();
	}
}
